#include <iostream>
#include <assert.h>
using namespace std;
template<class SLDateType>
class SeqList
{
public:
    // 对数据的管理:增删查改
    SeqList();//初始化
    SeqList(SLDateType* a, size_t n);
    void check_capacity();
    void SeqListPrint();//打印
    void SeqListPushBack(SLDateType x);//尾部插入
    void SeqListPushFront(SLDateType x);//头部插入
    void SeqListPopFront();//头删
    void SeqListPopBack();//尾删
// 顺序表查找
    int SeqListFind(SLDateType x);
    SLDateType Get(size_t i);
// 顺序表在pos位置插入x
    void SeqListInsert(size_t pos, SLDateType x);
// 顺序表删除pos位置的值
    void SeqListErase(size_t pos);
    ~SeqList();//销毁
private:
    SLDateType* _a;
    size_t _size;
    size_t _capacity;
};


template<typename SLDateType>
SeqList<SLDateType>::SeqList()
{
    assert(this);
    _a = NULL;
    _size = 0;
    _capacity = 0;
}
template<typename SLDateType>

SeqList<SLDateType>::SeqList(SLDateType* a, size_t n)
        :_a(new SLDateType[n * 2])
        ,_size(n)
        ,_capacity(n * 2)
{
    for (int i = 0; i < n; i++)
    {
        _a[i] = a[i];
    }
}
template<typename SLDateType>
SeqList<SLDateType>::~SeqList()
{
    assert(this);
    if (this->_a)
    {
        delete(this->_a);
        _size = 0;
        _capacity = 0;
    }
}

template<class SLDateType>
void SeqList<SLDateType>::check_capacity()
{
    assert(this);
    if (_size==_capacity)
    {
        int newCapacity = _capacity == 0 ? 4 : _capacity * 2;
        SLDateType* tmp=new SLDateType[newCapacity];
        memcpy(tmp,_a,_size*sizeof(SLDateType));//复制的字节数，根据数据类型变化
        delete[] _a;
        _a=tmp;
        _capacity = newCapacity;

    }
}

template<class SLDateType>
void SeqList<SLDateType>::SeqListPrint()
{
    assert(this);
    for (int i = 0; i < _size; i++)
    {
        cout<< _a[i]<< " ";
    }
    cout<<endl;
};

template<class SLDateType>
void SeqList<SLDateType>::SeqListPushBack(SLDateType x)
{
    SeqListInsert(_size, x);
}

template<class SLDateType>
void SeqList<SLDateType>::SeqListPushFront(SLDateType x)
{
    SeqListInsert( 0, x);
}
template<class SLDateType>
void SeqList<SLDateType>::SeqListPopFront()
{
    SeqListErase(0);
}

template<class SLDateType>
void SeqList<SLDateType>::SeqListPopBack()
{
    assert(this);
    assert(_size > 0);
    _size--;
}

// 顺序表查找
template<class SLDateType>
int SeqList<SLDateType>::SeqListFind(SLDateType x)
{
    assert(this);
    for (int i = 0; i < _size; i++)
    {
        if (_a[i] == x)
            return i;
    }
    return -1;
}
template<class SLDateType>
SLDateType SeqList<SLDateType>::Get(size_t i)
{
    assert(i >= 0);
    assert(i < _size);
    return _a[i];
}
// 顺序表在pos位置插入x
template<class SLDateType>
void SeqList<SLDateType>::SeqListInsert(size_t pos, SLDateType x)
{
    assert(this);
    check_capacity();
    for (int i = _size; i > pos; i--)
    {
        _a[i] = _a[i - 1];
    }
    _a[pos] = x;
    _size++;
}
// 顺序表删除pos位置的值
template<class SLDateType>
void SeqList<SLDateType>::SeqListErase(size_t pos)
{
    assert(this);
    assert(pos >= 0);
    assert(pos < _size);
    for (int i = pos; i < _size - 1; i++)
    {
        _a[i] = _a[i + 1];
    }
    _size--;
}